package com.future.utils;


import com.future.constant.JWTConstant;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

public final class JWTUtil {

    public static String createToken(String id, String name, Map<String, Object> claims) {
        long now = System.currentTimeMillis();
        long exp = now + JWTConstant.EXPIRATION_TIME;

        return Jwts.builder()
                .setClaims(claims)
                .setId(id)
                .setSubject(name)
                .setIssuedAt(new Date())
                .signWith(SignatureAlgorithm.HS256, JWTConstant.AUTOGRAPH)
                .setExpiration(new Date(exp))
                .compact();
    }

    public static Claims decrypt(String token) {
        return Jwts.parser()
                .setSigningKey(JWTConstant.AUTOGRAPH)
                .parseClaimsJws(token)
                .getBody();
    }
}
